package com.echo.yixiaowu.feature.main.home

import android.app.Application
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import com.echo.yixiaowu.feature.components.EditSectionsSheet
import com.echo.yixiaowu.feature.components.HomeContent
import com.echo.yixiaowu.main.page.rememberImageLoader
import com.echo.yixiaowu.route.AppRoute
import kotlinx.coroutines.launch

@OptIn(ExperimentalSharedTransitionApi::class, ExperimentalMaterial3Api::class)
@Composable
fun HomePage(
    sharedTransitionScope: SharedTransitionScope,
    animatedVisibilityScope: AnimatedVisibilityScope,
    homeViewModel: HomeViewModel = viewModel(
        factory = ViewModelProvider.AndroidViewModelFactory(
            LocalContext.current.applicationContext as Application
        )
    ),
    onNavigate: (AppRoute) -> Unit,
) {
    val sortState by homeViewModel.sortState.collectAsState()
    val recommendState by homeViewModel.recommendState.collectAsState()
    val coroutineScope = rememberCoroutineScope()
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)

    var showBottomSheet by remember { mutableStateOf(false) }
    val imageLoader = rememberImageLoader()

    // 编辑分区排序
    if (showBottomSheet) {
        EditSectionsSheet(
            sheetState = sheetState,
            sortState = sortState,
            onDismissRequest = { showBottomSheet = false },
            onConfirm = {
                coroutineScope.launch { sheetState.hide() }.invokeOnCompletion {
                    if (!sheetState.isVisible) {
                        showBottomSheet = false
                    }
                }
            },
            onEvent = homeViewModel::onEvent
        )
    }

    if (sortState.isLoading) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            CircularProgressIndicator()
        }
        return
    }

    // 主内容
    HomeContent(
        sortState = sortState,
        recommendState = recommendState,
        onShowBottomSheet = { showBottomSheet = true },
        onEvent = homeViewModel::onEvent,
        sharedTransitionScope = sharedTransitionScope,
        animatedVisibilityScope = animatedVisibilityScope,
        imageLoader = imageLoader,
        onNavigate = onNavigate
    )
}
